package rwlivbk.service;

import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import rwlivbk.constants.LiveConstants;
import rwlivbk.model.ChatMessage;
import rwlivbk.model.LiveMessage;
import rwlivbk.utils.JsonUtil;

@Service
public class ChatService {

    @Autowired
    private RabbitTemplate rabbitTemplate;

    public boolean sendMsg(String message) {
        if (message == null || message.trim().isEmpty()) {
            return false;
        }
        ChatMessage chatMessage = JsonUtil.parseJsonToObj(message, ChatMessage.class);
        if (chatMessage == null) {
            return false;
        }
        LiveMessage liveMessage = new LiveMessage();
        liveMessage.setType("CHAT");
        liveMessage.setContent("User [" + chatMessage.getSender() + "] says (from MQ): " + chatMessage.getContent());
        rabbitTemplate.convertAndSend(LiveConstants.STOMP_EXCHANGE, LiveConstants.STOMP_ROUTE_KEY, JsonUtil.toJson(liveMessage));
        return true;
    }
}
